package com.frogs.ucenter.v1.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.DirectMessageBatchUpdateRequest;
import com.frogs.ucenter.v1.request.DirectMessageCountRequest;
import com.frogs.ucenter.v1.request.DirectMessageListRequest;
import com.frogs.ucenter.v1.request.DirectMessageReadRequest;
import com.frogs.ucenter.v1.request.DirectMessageSendRequest;
import com.frogs.ucenter.v1.request.EmailSendRequest;
import com.frogs.ucenter.v1.request.SmsCheckRequest;
import com.frogs.ucenter.v1.request.SmsReceiveRequest;
import com.frogs.ucenter.v1.request.SmsVerifyRequest;
import com.frogs.ucenter.v1.service.DirectMessageService;
import com.frogs.ucenter.v1.service.EmailService;
import com.frogs.ucenter.v1.service.SmsService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * @author wushubin 
 * 通讯接口，包括站内短信、手机短信、邮件
 */
@Controller(ApiConstants.VERSION+"communicationController")
@RequestMapping("/"+ApiConstants.VERSION)
public class CommunicationController {
	
	@Resource(name=ApiConstants.VERSION+"smsService")
	private SmsService smsService;
	
	@Resource(name=ApiConstants.VERSION+"emailService")
	private EmailService emailService;
	
	@Resource(name=ApiConstants.VERSION+"directMessageService")
	private DirectMessageService directMessageService;

	private static final String SMS_SEND = "/sms/send";
	private static final String SMS_VERIFY_SEND = "/sms/verify/send";
	private static final String SMS_VERIFY_CHECK = "/sms/verify/check";
	private static final String EMAIL_SEND = "/email/send";
	private static final String DM_SEND = "/dm/send";
	private static final String DM_COUNT = "/dm/count";
	private static final String DM_LIST = "/dm/list";
	private static final String DM_READ = "/dm/read";
	private static final String DM_UPDATE = "/dm/update";
	
	@RequestMapping(SMS_SEND)
	public Response smsSend(HttpServletRequest request) {
		Response response;
		try {
			SmsReceiveRequest smsReceiveRequest = new SmsReceiveRequest();
			smsReceiveRequest.parse(request);
			response = smsService.sendMessage(smsReceiveRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping({SMS_VERIFY_SEND})
	public Response smsVerifySend(HttpServletRequest request) {
		Response response;
		try {
			SmsVerifyRequest smsVerifyRequest = new SmsVerifyRequest();
			smsVerifyRequest.parse(request);
			response = smsService.sendVerify(smsVerifyRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping({SMS_VERIFY_CHECK})
	public Response smsVerifyCheck(HttpServletRequest request) {
		Response response;
		try {
			SmsCheckRequest smsCheckRequest = new SmsCheckRequest();
			smsCheckRequest.parse(request);
			response = smsService.checkVerify(smsCheckRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping(EMAIL_SEND)
	public Response emailSend(HttpServletRequest request) {
		Response response;
		try {
			EmailSendRequest emailSendRequest = new EmailSendRequest();
			emailSendRequest.parse(request);
			response = emailService.send(emailSendRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(DM_SEND)
	public Response directMessageSend(HttpServletRequest request) {
		Response response;
		try {
			DirectMessageSendRequest directMessageSendRequest = new DirectMessageSendRequest();
			directMessageSendRequest.parse(request);
			response = directMessageService.send(directMessageSendRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping(DM_COUNT)
	public Response directMessageCount(HttpServletRequest request) {
		Response response;
		try {
			DirectMessageCountRequest directMessageCountRequest = new DirectMessageCountRequest();
			directMessageCountRequest.parse(request);
			response = directMessageService.countByStatus(directMessageCountRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping(DM_LIST)
	public Response directMessageList(HttpServletRequest request) {
		Response response;
		DirectMessageListRequest listRequest = new DirectMessageListRequest();
		listRequest.parse(request);
		try {
			response = directMessageService.getUserMessageDmList(listRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping(DM_READ)
	public Response directMessageRead(HttpServletRequest request) {
		Response response;
		DirectMessageReadRequest readRequest = new DirectMessageReadRequest();
		readRequest.parse(request);
		try {
			response = directMessageService.readUserMessageDm(readRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping(DM_UPDATE)
	public Response directMessageUpdate(HttpServletRequest request) {
		Response response;
		DirectMessageBatchUpdateRequest readRequest = new DirectMessageBatchUpdateRequest();
		readRequest.parse(request);
		try {
			response = directMessageService.batchUpdateUserMessageDm(readRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
